package com.aidong.home.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.activity.biz.MstCampaignBiz;
import com.aidong.activity.entity.Home;
import com.aidong.food.biz.MstFoodBiz;
import com.aidong.home.biz.TrnHomeBiz;
import com.aidong.home.entity.ActivityHome;
import com.aidong.home.entity.CourseHome;
import com.aidong.home.entity.FoodHome;
import com.aidong.home.entity.TrnHome;
import com.aidong.util.PageUtil;
import com.aidong.util.ReadPropertiesUtil;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json")
@Namespace("/")
public class TrnHomeAction implements ModelDriven<TrnHome> {

	private TrnHome trnHome;
	
	@Override
	public TrnHome getModel() {
		if (trnHome == null) {
			trnHome = new TrnHome();
		}
		return trnHome;
	}
	private Map<String, Object> map;

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	
	private MstCampaignBiz mstCampaignBiz; 
	@Autowired
	public void setMstCampaignBiz(MstCampaignBiz mstCampaignBiz) {
		this.mstCampaignBiz = mstCampaignBiz;
	} 
	private TrnHomeBiz trnHomeBiz; 
	@Autowired
	public void setTrnHomeBizBiz(TrnHomeBiz trnHomeBiz) {
		this.trnHomeBiz = trnHomeBiz;
	} 
	
	private MstFoodBiz mstFoodBiz;
	@Autowired
	public void setMstFoodBiz(MstFoodBiz mstFoodBiz) {
		this.mstFoodBiz = mstFoodBiz;
	} 
	/**
	 * 首页信息
	 * 
	 * @return
	 */
	@Action(value = "getHomeNew", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getMstFoodHome() {
		map = new HashMap<String, Object>();
		try{
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("cityId", trnHome.getCityId());
			paraMap.put("recmdSite", "shoppSite");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			List<Home> listHome=mstCampaignBiz.getCampaignBannerNew(paraMap);
			for(Home home:listHome){
				home.setRecmdSite("shoppSite");
			}
			resultMap.put("top", listHome);//轮播图
			FoodHome foodHome=new FoodHome();
			paraMap.put("recmdSite", "shoppRecommend");
			List<Home> shopHome=mstCampaignBiz.getCampaignBannerNew(paraMap);
			if(shopHome.size()>0){
				for(Home home:shopHome){
					home.setRecmdSite("shoppRecommend");
				}
				paraMap.put("recmdSite", "newShoppSite");
				List<Home> li=mstCampaignBiz.getCampaignBannerNew(paraMap);//商品推荐位图片
				
				if(li.size()>0){
					foodHome.setHomeId(li.get(0).getRecmdId());
					foodHome.setImgUrl(li.get(0).getRecmdUrl());
				}
			}
			foodHome.setFoodList(shopHome);//推荐商品位商品
			
			//首页课程推荐
			List<CourseHome> courseHome=trnHomeBiz.getTrnCourseHome(paraMap);
			//首页活动推荐
			List<ActivityHome> activityHome=trnHomeBiz.getTrnActivityHome(paraMap);
			
			
			resultMap.put("course", courseHome);
			resultMap.put("campaign", activityHome);
			resultMap.put("food",foodHome);//商品推荐位商品
			map.put("code", 1);
			map.put("result", resultMap);
			return "succ";
		} catch(Exception e) {
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	/**
	 * 商品推荐（大图）
	 * 
	 * @return
	 */
	@Action(value = "getHomeFoodDetail", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getHomeFoodDetail() {
		map = new HashMap<String, Object>();
		if(trnHome.getHomeId() > 0){
			try{

				Map<String, Object> paraMaps = new HashMap<String, Object>();
				paraMaps.put("homeId", trnHome.getHomeId());
				Map<String,Object> detailMap=trnHomeBiz.getTrnFoodHomeDetail(paraMaps);
				detailMap.put("imgUrl",ReadPropertiesUtil.getIMGREAD(detailMap.get("imgUrl")));
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("cityId", trnHome.getCityId());
				paraMap.put("recmdSite", "shoppRecommend");
				Map<String, Object> resultMap = new HashMap<String, Object>();
				
				resultMap.put("detail", detailMap);
				resultMap.put("foodList", mstCampaignBiz.getCampaignBannerNew(paraMap));
				map.put("code", 1);
				map.put("result", resultMap);
				return "succ";
			} catch(Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
	/**
	 * 商品推荐下边的品牌列表
	 * 
	 * @return
	 */
	@Action(value = "getFoodListByBrandId", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getFoodListByBrandId() {
		map = new HashMap<String, Object>();
		if(trnHome.getConOrActID() > 0 && !StringUtils.isBlank(trnHome.getTypeName())){
			try{
				Map<String, Object> paraMap = PageUtil.page(10, trnHome.getPageCurrent());
				String brandDict = "";
				int goodsType=0;
				if ("31".equals(trnHome.getTypeName())) {
					brandDict = "foodBrandDict";
					goodsType=3;
				} else if ("39".equals(trnHome.getTypeName())) {
					brandDict = "nourishmentBrandDict";
					goodsType=6;
				} else if ("40".equals(trnHome.getTypeName())) {
					goodsType=7;
					brandDict = "equipBrandDict";
				}
				paraMap.put("brandDict", brandDict);
				paraMap.put("brandId", trnHome.getConOrActID());
				paraMap.put("pBrandId", goodsType);
				paraMap.put("cityId", trnHome.getCityId());
				List<Map<String, Object>> rules = new ArrayList<Map<String, Object>>();
				rules = mstFoodBiz.getMstSupplyRuleNew(paraMap);
				if (rules.size() > 0) {
					for (Map<String, Object> rule : rules) {
						String picUrl = rule.get("gpPic") + "";
						//rule.put("gpPic", ReadPropertiesUtil.getIMGREAD(picUrl));
						rule.put("gpPic", ReadPropertiesUtil.get300Img(picUrl));
					}
				}
				//Map<String,Object> resultMap=trnHomeBiz.getTrnFoodHomeDetail(paraMap);
				map.put("code", 1);
				map.put("result", rules);
				return "succ";
			} catch(Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
}
